﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using BPM.ViewModel;
using BPM.Entity;
using BPM.Service;
using hxy.CopyRight;

namespace BPM.Web
{
	public class InternalViewController : BaseController
	{
		protected override Role[] GetCheckRoles()
		{
			return new Role[]{
				SysRoles.Admin, SysRoles.AssembleManager, SysRoles.AssembleStaff,
				SysRoles.Buyer, SysRoles.Manager, SysRoles.MaterialStaff,
				SysRoles.StorageManager, SysRoles.Supplier
			};
		}

		protected override ViewResult View(IView view, object model)
		{
			this.InitSharedViewModels();

			return base.View(view, model);
		}

		protected override ViewResult View(string viewName, string masterName, object model)
		{
			this.InitSharedViewModels();

			return base.View(viewName, masterName, model);
		}

		private void InitSharedViewModels()
		{
			this.ViewData["UserName"] = this.CurrentUser.UserName;

			this.PrepareMenu();

			this.PrepareMaterials();
		}

		private void PrepareMaterials()
		{
			//set materials data
			var mdSvc = new MaterialDefinitionService();
			var allMaterial = mdSvc.GetAll();

			var svc = new MaterialDefinitionCategoryService();
			var bigCates = svc.GetBigCates().Select(c => new
			{
				value = c.Id,
				text = c.Name,
				subCates = c.SubCategoryList.Select(sc => new
				{
					pid = c.Id,
					value = sc.Id,
					text = sc.Name,
					materials = allMaterial.Where(m => m.CategoryId == sc.Id).Select(m => new
					{
						pid = sc.Id,
						bigCateId = c.Id,
						value = m.Id,
						text = m.Name
					}).ToArray()
				}).ToArray()
			});

			this.ViewData["IVC_AllMaterials"] = JsonString(bigCates);
		}

		#region 用户权限

		protected void PrepareMenu()
		{
			var menu = new List<MenuViewModel>();
			MenuViewModel currentMenu = null;

			var curUser = this.CurrentUser;
			//添加菜单
			if (curUser.HasRole(SysRoles.Admin))
			{
				this.AddMenuItems(menu);
			}
			else
			{
				if (curUser.RoleList.Count > 0)
				{
					MenuType[] menuEnabled = null;
					if (MenuViewModel.EnabledMenus.TryGetValue(curUser.RoleList[0].Id, out menuEnabled))
					{
						this.AddMenuItems(menu, menuEnabled);
					}
				}
			}

			//设置当前菜单
			var controller = this.RouteData.GetControllerName();
			var action = this.RouteData.GetActionName();
			currentMenu = menu.FirstOrDefault(m => m.ControllerName == controller && m.ActionName == action);

			this.ViewData["Menu"] = menu;
			this.ViewData["CurrentMenu"] = currentMenu;
		}

		private void AddMenuItems(List<MenuViewModel> items)
		{
			items.AddRange(AvailableMenus);
		}

		private void AddMenuItems(List<MenuViewModel> items, params MenuType[] types)
		{
			foreach (var type in types)
			{
				var menu = AvailableMenus.FirstOrDefault(m => m.Type == type);
				if (menu != null)
				{
					items.Add(menu);
				}
			}
		}

		#endregion

		public static readonly IList<MenuViewModel> AvailableMenus = new ReadOnlyCollection<MenuViewModel>(new MenuViewModel[]{
            Menu(MenuCategory.系统管理, MenuType.用户管理, ad => ad.Authority.UserList),
            new MenuViewModel(MenuCategory.系统管理, MenuType.日志管理, "ViewUserList", "OperationLog"),
            Menu(MenuType.物料类别管理, a => a.Category.CategoryList),
            Menu(MenuType.添加物料, a => a.MaterialManagement.AddMaterial),
            Menu(MenuType.现有物料管理, a => a.MaterialManagement.ViewMaterialList),
            Menu(MenuType.审批物料, a => a.MaterialManagement.ViewPendingMaterialList),
            Menu(MenuType.批量导入物料, a => a.MaterialManagement.ImportMaterialList),
            Menu(MenuType.新建进货单, a => a.PurchaseOrder.CreatePurchaseOrder),
            Menu(MenuType.审批进货单, a => a.PurchaseOrder.PendingPurchaseOrderList),
            Menu(MenuType.我的进货单_采购员, a => a.PurchaseOrder.MyPurchaseOrderList),
            Menu(MenuType.进货单_库管员, a => a.PurchaseOrder.AllApprovedPurchaseOrderListByStorager),
            Menu(MenuType.查看所有物料, a => a.Search.Storage),
            Menu(MenuType.物料入库, a => a.MaterialInDelivery.MaterialInDelivery),
            Menu(MenuType.待审核入库申请, a => a.MaterialInDelivery.PendingMaterialInDeliveryList),
            Menu(MenuType.物料出库, a => a.MaterialDelivery.MaterialDelivery),
            Menu(MenuType.待审核出库申请, a => a.MaterialDelivery.PendingMaterialDeliveryList),
            Menu(MenuType.所有产品, a => a.Assemble.ProductDefinitionList),
            Menu(MenuType.成品组装定义, a => a.Assemble.AddProductDefinitionAssemble),
			//Menu(MenuType.成品组装, a => a.Assemble.ProductAssemble),
            Menu(MenuCategory.其它, MenuType.退出, a => a.Logon.Logout),
        });

		private static MenuViewModel Menu(MenuType type, Expression<Func<AD, string>> actionExp)
		{
			return Menu(MenuCategory.进销存, type, actionExp);
		}
		private static MenuViewModel Menu(MenuCategory category, MenuType type, Expression<Func<AD, string>> actionExp)
		{
			var r = AD.Read(actionExp);

			return new MenuViewModel(category, type, r.Action, r.Controller);
		}
	}
}